package book;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import bookshopSM.DBConnection;

public class BookManager extends DBConnection {
    private  Connection conn=null;
    //构造方法中完成打开数据库链接
    public BookManager() {
        conn=getConnection();  //调用父类方法，链接数据库，返回Connection对象
    }
    //对student表进行查询，并将查询结果集以List结构返回，并及时关闭数据库连接
    public List<Book> Query(String sqlString) throws SQLException
    {
        List<Book> list = new ArrayList<Book>();
        Statement statement=null;
        ResultSet rs=null;
        try {
            statement = conn.createStatement();
            rs= statement.executeQuery(sqlString);
            while(rs.next()) {
                Book m=new Book();
                m.setBname(rs.getString("Bname"));
                m.setBcover(rs.getString("Bcover"));
                m.setBauthor(rs.getString("Bauthor"));
                m.setBno(rs.getInt("Bno"));
                m.setBprice(rs.getFloat("Bprice"));
                m.setBsellprice(rs.getFloat("Bsellprice"));
                m.setBsort(rs.getString("Bsort"));
                m.setBnum(rs.getInt("Bnum"));
                m.setBpress(rs.getString("Bpress"));
                m.setBnote(rs.getString("Bnote"));
                list.add(m);
            }
        }
        catch (SQLException e)
        { System.out.println(e); }
        finally {
            if (statement !=null)
                statement .close();
            if (rs!=null)
                rs.close();
            closeConnection();
        }
        return list;
    }
    //对任一表进行插入、修改、删除操作
    public int execUpdate(String sql)throws SQLException {
        int result=0;
        Statement statement=null;
        try {
            statement = conn.createStatement();
            result = statement.executeUpdate(sql);
        }
        catch (SQLException e)
        { System.out.println(e); }
        finally {
            if (statement!=null)
                statement.close();
            closeConnection();
        }
        return result;
    }
}

